package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yoomiito.app.R;
import com.yoomiito.app.utils.ResUtil;

import butterknife.BindView;
import butterknife.ButterKnife;


/************************************************************
 * ,* Author:  linqiang
 * ,* Description:  自定义了一条常用的点击的view
 * ,* Date: 2016/3/30
 ************************************************************/
public class ItemView extends LinearLayout {
    @BindView(R.id.leftIv)
    ImageView leftIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.desTv1)
    TextView desTv1;
    @BindView(R.id.desTv2)
    TextView desTv2;
    @BindView(R.id.outLin)
    LinearLayout outLin;
    @BindView(R.id.rightTv)
    TextView rightTv;
    @BindView(R.id.desRightTv)
    TextView desRightTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.view_divide)
    View bottomLine;
    @BindView(R.id.inLin)
    LinearLayout inLin;

    private Context mContext;


    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setBackgroundResource(R.color.color_white);
        initView(attrs);
    }

    public void setOnLeftClickListener(OnClickListener listener) {
        titleTv.setOnClickListener(listener);
    }

    private void initView(AttributeSet attrs) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_item,
                null, false);
        ViewGroup.LayoutParams params = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        this.addView(view, params);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        LinearLayout outLin = (LinearLayout) view.findViewById(R.id.outLin);


        TypedArray typedArray = mContext.obtainStyledAttributes(attrs,
                R.styleable.ItemView, 0, 0);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            int attr = typedArray.getIndex(i);

            switch (attr) {
                /**
                 * 设置左边图片
                 */
                case R.styleable.ItemView_leftImageView:
                    int leftResourceId = typedArray.getResourceId(attr, 0);
                    leftIv.setImageResource(leftResourceId);
                    leftIv.setVisibility(VISIBLE);
                    break;
                /**
                 * 设置右边图片
                 */
                case R.styleable.ItemView_rightImageView:
                    int rightResourceId = typedArray.getResourceId(attr, 0);
                    rightIv.setImageResource(rightResourceId);
                    rightIv.setVisibility(VISIBLE);
                    break;
                /**
                 * 设置中间文字
                 */
                case R.styleable.ItemView_titleText:
                    String leftContent = typedArray.getString(attr);
                    titleTv.setText(leftContent);
                    titleTv.setVisibility(VISIBLE);
                    break;

                /**
                 * 设置描述语1文字
                 */
                case R.styleable.ItemView_des1Text:
                    String content = typedArray.getString(attr);
                    desTv1.setText(content);
                    desTv1.setVisibility(VISIBLE);
                    break;
                /**
                 * 设置描述语2文字
                 */
                case R.styleable.ItemView_des2Text:
                    String des2content = typedArray.getString(attr);
                    desTv2.setText(des2content);
                    desTv2.setVisibility(VISIBLE);
                    break;
                /**
                 * 设置右边文字
                 */
                case R.styleable.ItemView_rightText:
                    String rightContent = typedArray.getString(attr);
                    rightTv.setText(rightContent);
                    rightTv.setVisibility(VISIBLE);
                    break;


                /**
                 * 右边图片显示情况
                 */
                case R.styleable.ItemView_leftImageVisibility:
                    int v1 = typedArray.getInteger(attr, 0);
                    if (getResources().getInteger(R.integer.visible) == v1) {
                        leftIv.setVisibility(VISIBLE);
                    } else if (getResources().getInteger(R.integer.inVisible) == v1) {
                        leftIv.setVisibility(INVISIBLE);
                    } else {
                        leftIv.setVisibility(GONE);
                    }
                    break;
                /**
                 * 右边图片显示情况
                 */
                case R.styleable.ItemView_rightImageVisibility:
                    int v2 = typedArray.getInteger(attr, 0);
                    if (getResources().getInteger(R.integer.visible) == v2) {
                        rightIv.setVisibility(VISIBLE);
                    } else if (getResources().getInteger(R.integer.inVisible) == v2) {
                        rightIv.setVisibility(INVISIBLE);
                    } else {
                        rightIv.setVisibility(GONE);
                    }

                    break;
                /**
                 * 设置右边文字颜色
                 * */
                case R.styleable.ItemView_rightColor:
                    rightTv.setTextColor(typedArray.getColor(attr, 0x000000));
                    break;
                /**
                 * 设置中间文字颜色
                 * */
                case R.styleable.ItemView_titleColor:
                    titleTv.setTextColor(typedArray.getColor(attr, 0x000000));
                    break;
                /**
                 * 设置中间文字大小
                 * */
                case R.styleable.ItemView_titleSize:
                    if (typedArray.getDimension(attr, 0) > 0) {
                        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
                        titleTv.setTextSize((int) (typedArray.getDimension(attr, 0) / fontScale + 0.5f));
                    }

                    break;
                /**
                 * 设置右边文字大小
                 * */
                case R.styleable.ItemView_rightSize:
                    if (typedArray.getDimension(attr, 0) > 0) {
                        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
                        rightTv.setTextSize((int) (typedArray.getDimension(attr, 0) / fontScale + 0.5f));
                    }

                    break;
                /**
                 * 设置下面的线显示情况
                 */
                case R.styleable.ItemView_bottomLineVisibility:
                    int v3 = typedArray.getInteger(attr, 0);
                    if (getResources().getInteger(R.integer.visible) == v3) {
                        bottomLine.setVisibility(VISIBLE);
                    } else if (getResources().getInteger(R.integer.inVisible) == v3) {
                        bottomLine.setVisibility(INVISIBLE);
                    } else {
                        bottomLine.setVisibility(GONE);
                    }
                    break;
                /**
                 *
                 */
                case R.styleable.ItemView_itemType:
                    int v4 = typedArray.getInteger(attr, 0);
                    if (getResources().getInteger(R.integer.typeShowText) == v4) {
                        rightIv.setVisibility(GONE);
                        rightTv.setVisibility(VISIBLE);
                        inLin.setPadding(0, 0, 0, 0);
                        outLin.setPadding(5, 5, 5, 5);
                    } else if (getResources().getInteger(R.integer.typeMain) == v4) {
                        outLin.setPadding(0,ResUtil.getSize(R.dimen.dimen_15dp),0,ResUtil.getSize(R.dimen.dimen_15dp));
                        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,ResUtil.getSizeFloat(R.dimen.dimen_16sp));
                        desTv1.setTextSize(TypedValue.COMPLEX_UNIT_PX,ResUtil.getSizeFloat(R.dimen.dimen_13sp));
                        desTv2.setTextSize(TypedValue.COMPLEX_UNIT_PX,ResUtil.getSizeFloat(R.dimen.dimen_13sp));
                        leftIv.setPadding(ResUtil.getSize(R.dimen.dimen_15dp),0,ResUtil.getSize(R.dimen.dimen_15dp),0);
                        rightIv.setPadding(0,0,ResUtil.getSize(R.dimen.dimen_15dp),0);

                        LinearLayout.LayoutParams paramTest = (LinearLayout.LayoutParams) inLin.getLayoutParams();
                        paramTest.leftMargin = 0;
                        paramTest.rightMargin = 0;
                        inLin.setLayoutParams(paramTest);
                    }
                    break;

                /**
                 * 设置titleView的图片
                 */
                case R.styleable.ItemView_titleImage:
                    Drawable rightDrawable = typedArray.getDrawable(attr);
                    rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
                    titleTv.setCompoundDrawables(null, null, rightDrawable, null);
                    break;
            }
        }
        typedArray.recycle();
    }

    public void setLeftImageVisibility(int value) {
        leftIv.setVisibility(value);
    }

    public void setLeftImage(int value) {
        leftIv.setVisibility(View.VISIBLE);
        leftIv.setImageResource(value);
    }

    public void setTitleText(String value) {
        titleTv.setVisibility(View.VISIBLE);
        titleTv.setText(value);
    }

    public void setDes1Text(String value) {
        desTv1.setVisibility(View.VISIBLE);
        desTv1.setText(value);
    }

    public void setDes2Text(String value) {
        desTv2.setVisibility(View.VISIBLE);
        desTv2.setText(value);
    }

    public String getRightText() {
        return rightTv.getText().toString();
    }

    public void setRightText(String value) {
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText(value);
    }

    public void setRightTextColor(int color) {
        rightTv.setTextColor(color);
    }

    public void setDesRightText(String value){
        desRightTv.setVisibility(VISIBLE);
        desRightTv.setText(value);
    }

    public void setDesRightTvColor(int color){
        desRightTv.setTextColor(color);
    }

    public void setDesRightTvBackground(int resId){
        desRightTv.setBackgroundResource(resId);
    }

    public void setRightImage(int value) {
        rightIv.setVisibility(View.VISIBLE);
        rightIv.setImageResource(value);
    }

    public void setBottomLineVisibility(int value) {
        bottomLine.setVisibility(value);
    }

    public TextView getTitleTextView() {
        return titleTv;
    }

    public void setRightTvVisibility(int value) {
        rightTv.setVisibility(value);
    }

    public void setTitleVisibility(int value) {
        titleTv.setVisibility(value);
    }

    public void setDes1Visibility(int value) {
        desTv1.setVisibility(value);
    }

    public void setDes2Visibility(int value) {
        desTv2.setVisibility(value);
    }

    public void setRightImageVisibility(int value) {
        rightIv.setVisibility(value);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
